﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Selenium;
using SeleniumTestProject.Tests.EntityStore;
using System.Configuration;
using SeleniumTestProject.Properties;
using ThoughtWorks.Selenium.UnitTests;
using PageLibrary.Pages.COM;
using PageLibrary.Page.Elements;
using PageLibrary;


namespace SeleniumTestProject.Tests.Core
{
    class Core
    {
        private ISelenium selenium;
        private Int32 timeToWaitForPageLoad;
        private PaymentCardDetails pcd;
        private UserAddressInfo uai;
        private SearchParametres sp;
        private LoginParameters lp;
        private POSSelector selector = new POSSelector();

        public Core(Int32 timeToWaitForPageLoad, PaymentCardDetails pcd, UserAddressInfo uai, SearchParametres sp, LoginParameters lp)
        {
            selenium = new DefaultSelenium(ConfigurationSettings.AppSettings["Settings_host"].ToString(),
                Int32.Parse(ConfigurationSettings.AppSettings["Settings_port"].ToString()),
                ConfigurationSettings.AppSettings["Settings_browser"].ToString(),
                ConfigurationSettings.AppSettings["Settings_site"].ToString());
            selenium.Start();
            selenium.Open("/");
            selenium.WaitForPageToLoad(timeToWaitForPageLoad.ToString());
            this.timeToWaitForPageLoad = timeToWaitForPageLoad;
            this.pcd = pcd;
            this.uai = uai;
            this.sp = sp;
            this.lp = lp; 
        }

        public Int32 TimeToWaitForPageLoad
        {
            get { return timeToWaitForPageLoad; }
            set { timeToWaitForPageLoad = value; }
        }

        public ISelenium Selenium
        {
            get { return this.selenium; }
        }

        private void CheckLogin()
        {
            if (selenium.IsTextPresent("Sign in or select an option"))
            {
                 this.Login();                 
            }
        }

        private void Login()
        {
            LoginPage loginPage = new LoginPage_COM();
            selenium.Type(loginPage.GetPageElementByLabel(LoginPageElements.Login), lp.LoginName);
            selenium.Type(loginPage.GetPageElementByLabel(LoginPageElements.Password), lp.LoginPassword);
            selenium.Click(loginPage.GetPageElementByLabel(LoginPageElements.Confirm));
            selenium.WaitForPageToLoad(timeToWaitForPageLoad.ToString());            
        }
        #region Deprecated Step01
        
        
        //public void Step01_SetFlightRoundTripSearchingAttributes()
        //{
        //    if (ConfigurationSettings.AppSettings["Settings_site"].ToString() == "http://www.expedia.be/")
        //    {
        //        selenium.Click(selector.GetTestDataByKey("SearchParametersPage_SelectFlightsTrip"));
        //        selenium.Select(selector.GetTestDataByKey("SetSearchParametersPage_HumanNumber"), "1");
        //    }

        //    SearchPage searchPage = new SearchPage_COM();
        //    searchPage.GetPageElementByLabel(SearchPageElements.CityFrom);


        //    selenium.Type(selector.GetTestDataByKey("SearchParametersPage_CityFromField"), sp.CityFrom);
        //    selenium.Type(selector.GetTestDataByKey("SearchParametersPage_CityToField"), sp.CityTo);
        //    selenium.Type(selector.GetTestDataByKey("SearchParametersPage_DateFromField"), sp.DateFrom);
        //    selenium.Type(selector.GetTestDataByKey("SearchParametersPage_DateToField"), sp.DateReturn);
        //    selenium.Click(selector.GetTestDataByKey("SearchParametersPage_SubmitSearch"));
        //    selenium.WaitForPageToLoad(timeToWaitForPageLoad.ToString());
        //    this.CheckLogin();
        //}
        #endregion

        public void Step01()
        {
            SearchPage searchPage = new SearchPage();
            if (ConfigurationSettings.AppSettings["Settings_site"].ToString() == "http://www.expedia.be/")
            {
                searchPage = new SearchPage_BE();
                selenium.Click(searchPage.GetPageElementByLabel(SearchPageElements.FlightType));
                selenium.Select(searchPage.GetPageElementByLabel(SearchPageElements.HumanCount), "1");
            }
            else if (ConfigurationSettings.AppSettings["Settings_site"].ToString() == "http://www.expedia.com/")
            {
                searchPage = new SearchPage_COM();
            }

            selenium.Type(searchPage.GetPageElementByLabel(SearchPageElements.CityFrom), sp.CityFrom);
            selenium.Type(searchPage.GetPageElementByLabel(SearchPageElements.CityTo), sp.CityTo);
            selenium.Type(searchPage.GetPageElementByLabel(SearchPageElements.DateFrom), sp.DateFrom);
            selenium.Type(searchPage.GetPageElementByLabel(SearchPageElements.DateRet), sp.DateReturn);
            selenium.Click(searchPage.GetPageElementByLabel(SearchPageElements.Search));
            selenium.WaitForPageToLoad(timeToWaitForPageLoad.ToString());
            this.CheckLogin();

        }

        #region Deprecated Step02
        //public void Step02_SelectFoundDepartingFlight()
        //{
        //    selenium.Click(selector.GetTestDataByKey("SeletionDepartmentFlightPage_DepFlight"));
        //    selenium.WaitForPageToLoad(timeToWaitForPageLoad.ToString());
        //    this.CheckLogin();
        //} 
        #endregion

        public void Step02()
        {
            SearchResultsPage resultsPage = new SearchResultsPage();
            if (ConfigurationSettings.AppSettings["Settings_site"].ToString() == "http://www.expedia.com/")
                resultsPage = new SearchResultsPage_COM();
            else if (ConfigurationSettings.AppSettings["Settings_site"].ToString() == "http://www.expedia.be/")
                resultsPage = new SearchResultsPage_BE();
            selenium.Click(resultsPage.GetPageElementByLabel(SearchResultsPageElements.DepartmentFlight));
            selenium.WaitForPageToLoad(timeToWaitForPageLoad.ToString());
            this.CheckLogin();
        }

        #region Deprecated Step03
        //public void Step03_SelectFoundReturningFlight()
        //{
        //    if (ConfigurationSettings.AppSettings["Settings_site"].ToString() == "http://www.expedia.com/")
        //    {                
        //        selenium.Click(selector.GetTestDataByKey("SeletionReturningFlightPage_RetFlight"));
        //        selenium.WaitForPageToLoad(timeToWaitForPageLoad.ToString());
        //        this.CheckLogin();
        //    }
        //    else if(ConfigurationSettings.AppSettings["Settings_site"].ToString() == "http://www.expedia.be/")
        //    {
        //        selenium.Check(selector.GetTestDataByKey("AgreementPage_CheckboxOption"));
        //        selenium.Click(selector.GetTestDataByKey("ChoiceconfirmationPage_Confirm"));
        //        selenium.WaitForPageToLoad(timeToWaitForPageLoad.ToString());
        //        this.CheckLogin();
        //    }
        //}
        #endregion

        public void Step03()
        {
            if (ConfigurationSettings.AppSettings["Settings_site"].ToString() == "http://www.expedia.com/")
            {
                SearchResultsPage resultsPage = new SearchResultsPage_COM();
                selenium.Click(resultsPage.GetPageElementByLabel(SearchResultsPageElements.ReturnFlight));
                selenium.WaitForPageToLoad(timeToWaitForPageLoad.ToString());
                this.CheckLogin();
            }
            else if (ConfigurationSettings.AppSettings["Settings_site"].ToString() == "http://www.expedia.be/")
            {
                AgreementPage agreementPageBE = new AgreementPage_BE();
                selenium.Check(agreementPageBE.GetPageElementByLabel(AgreementPageElements.AgreeOption));
                selenium.Click(agreementPageBE.GetPageElementByLabel(AgreementPageElements.Confirm));
                selenium.WaitForPageToLoad(timeToWaitForPageLoad.ToString());
                this.CheckLogin();
            }
        }

        #region Deprecated Step04
        //public void Step04_CheckAgreementAndGoOn()
        //{
        //    if (ConfigurationSettings.AppSettings["Settings_site"].ToString() == "http://www.expedia.com/")
        //        selenium.Check(selector.GetTestDataByKey("AgreementPage_CheckboxOption"));
        //    selenium.Click(selector.GetTestDataByKey("AgreementPage_GoToNextPageLink"));
        //    selenium.WaitForPageToLoad(timeToWaitForPageLoad.ToString());
        //    this.CheckLogin();
        //} 
        #endregion

        public void Step04()
        {
            AgreementPage agreementPage = new AgreementPage();
            if (ConfigurationSettings.AppSettings["Settings_site"].ToString() == "http://www.expedia.com/")
            {
                agreementPage = new AgreementPage_COM();
                selenium.Check(agreementPage.GetPageElementByLabel(AgreementPageElements.AgreeOption));
                selenium.Click(agreementPage.GetPageElementByLabel(AgreementPageElements.Confirm));
            }
            else if (ConfigurationSettings.AppSettings["Settings_site"].ToString() == "http://www.expedia.be/")
            {
                agreementPage = new ReadAgreementPage_BE();
                selenium.Click(agreementPage.GetPageElementByLabel(AgreementPageElements.ReadAgreement_BE));
            }
            
            selenium.WaitForPageToLoad(timeToWaitForPageLoad.ToString());
            selenium.WaitForPageToLoad(timeToWaitForPageLoad.ToString());
            this.CheckLogin();
        }

        #region Deprecated Step05
        //public void Step05_FillUserDetailsAfterAgreement()
        //{
        //    if (ConfigurationSettings.AppSettings["Settings_site"].ToString() == "http://www.expedia.com/")
        //        selenium.Select(selector.GetTestDataByKey("UserDetailsPage_UserNameAndSecondNameField"), pcd.UserName + " " + pcd.UserSecondName);
        //    else if (ConfigurationSettings.AppSettings["Settings_site"].ToString() == "http://www.expedia.be/")
        //        selenium.Select(selector.GetTestDataByKey("UserDetailsPage_UserNameAndSecondNameField"), "value=28");
        //    selenium.Type(selector.GetTestDataByKey("UserDetailsPage_UserNameField"), pcd.UserName);
        //    selenium.Type(selector.GetTestDataByKey("UserDetailsPage_UserSecondNameField"), pcd.UserSecondName);
        //    if (ConfigurationSettings.AppSettings["Settings_site"].ToString() == "http://www.expedia.com/")
        //        selenium.Type(selector.GetTestDataByKey("UserDetailsPage_UserCountryFromField"), uai.Country);
        //    selenium.Type(selector.GetTestDataByKey("UserDetailsPage_UserAreaField"), uai.Area);
        //    selenium.Type(selector.GetTestDataByKey("UserDetailsPage_UserExtNumberField"), uai.Ext);
        //    selenium.Type(selector.GetTestDataByKey("UserDetailsPage_UserPhoneField"), uai.Phone);
        //    selenium.Click(selector.GetTestDataByKey("UserDetailsPage_GoToNextPageField"));
        //    selenium.WaitForPageToLoad(timeToWaitForPageLoad.ToString());
        //    this.CheckLogin();
        //} 
        #endregion

        public void Step05()
        {
            UserDetailsPage userDetailsPage = new UserDetailsPage();
            if (ConfigurationSettings.AppSettings["Settings_site"].ToString() == "http://www.expedia.com/")
            {
                userDetailsPage = new UserDetailsPage_COM();
                selenium.Select(userDetailsPage.GetPageElementByLabel(UserDetailsPageElements.UserNameAndSecondName), pcd.UserName + " " + pcd.UserSecondName);
            }
            else if (ConfigurationSettings.AppSettings["Settings_site"].ToString() == "http://www.expedia.be/")
            {
                userDetailsPage = new UserDetailsPage_BE();
                selenium.Select(userDetailsPage.GetPageElementByLabel(UserDetailsPageElements.UserNameAndSecondName), "value=28");
            }
            selenium.Type(userDetailsPage.GetPageElementByLabel(UserDetailsPageElements.UserName), pcd.UserName);
            selenium.Type(userDetailsPage.GetPageElementByLabel(UserDetailsPageElements.UserSecondName), pcd.UserSecondName);
            if (ConfigurationSettings.AppSettings["Settings_site"].ToString() == "http://www.expedia.com/")
                selenium.Type(userDetailsPage.GetPageElementByLabel(UserDetailsPageElements.UserCountry), uai.Country);
            selenium.Type(userDetailsPage.GetPageElementByLabel(UserDetailsPageElements.UserArea), uai.Area);
            selenium.Type(userDetailsPage.GetPageElementByLabel(UserDetailsPageElements.UserExt), uai.Ext);
            selenium.Type(userDetailsPage.GetPageElementByLabel(UserDetailsPageElements.UserPhone), uai.Phone);
            selenium.Click(userDetailsPage.GetPageElementByLabel(UserDetailsPageElements.Confirm));
            selenium.WaitForPageToLoad(timeToWaitForPageLoad.ToString());
            this.CheckLogin();
        }

        #region Deprecated Step06
        //public void Step06_FillPaymentDetails()
        //{
        //    if (ConfigurationSettings.AppSettings["Settings_site"].ToString() == "http://www.expedia.com/")
        //    {
        //        selenium.Select(selector.GetTestDataByKey("PaymentFillingPage_CardTypeField"), pcd.UserSelectedCardType);
        //        selenium.Type(selector.GetTestDataByKey("PaymentFillingPage_CardNumberField"), pcd.UserCardNumber);
        //        selenium.Select(selector.GetTestDataByKey("PaymentFillingPage_CardExpirationDateMonth"), pcd.ExpirationDateMonth);
        //        selenium.Select(selector.GetTestDataByKey("PaymentFillingPage_CardExpirationDateYear"), pcd.ExpirationDateYear);
        //        selenium.Type(selector.GetTestDataByKey("PaymentFillingPage_UserNameField"), pcd.UserName);
        //        selenium.Type(selector.GetTestDataByKey("PaymentFillingPage_UserSecondNameField"), pcd.UserSecondName);
        //        selenium.Type(selector.GetTestDataByKey("PaymentFillingPage_AddressStreetField"), uai.Street);
        //        selenium.Type(selector.GetTestDataByKey("PaymentFillingPage_AddressCityField"), uai.City);
        //        selenium.Select(selector.GetTestDataByKey("PaymentFillingPage_AddressStateField"), uai.State);
        //        selenium.Type(selector.GetTestDataByKey("PaymentFillingPage_AddressZipCodeField"), uai.ZipCode);
        //        selenium.Type(selector.GetTestDataByKey("PaymentFillingPage_AddressAreaField"), uai.Area);
        //        selenium.Type(selector.GetTestDataByKey("PaymentFillingPage_AddressPhoneField"), uai.Phone);
        //        selenium.Type(selector.GetTestDataByKey("PaymentFillingPage_AddressExtField"), uai.Ext);
        //        selenium.Click(selector.GetTestDataByKey("PaymentFillingPage_SubmitPurchaseButton"));
        //        selenium.WaitForPageToLoad(timeToWaitForPageLoad.ToString());
        //        this.CheckLogin();
        //    }
        //    else if (ConfigurationSettings.AppSettings["Settings_site"].ToString() == "http://www.expedia.be/")
        //    {
        //        selenium.Select(selector.GetTestDataByKey("SecondUserDetailsPage_UserSelect"), pcd.UserName + " " + pcd.UserSecondName);
        //        selenium.Type(selector.GetTestDataByKey("SecondUserDetailsPage_UserName"), pcd.UserName);
        //        selenium.Type(selector.GetTestDataByKey("SecondUserDetailsPage_UserSecondName"), pcd.UserSecondName);
        //        selenium.Type(selector.GetTestDataByKey("SecondUserDetailsPage_UserCountry"), uai.Country);
        //        selenium.Click(selector.GetTestDataByKey("SecondUserDetailsPage_GoToNextPageLink"));
        //        selenium.WaitForPageToLoad(timeToWaitForPageLoad.ToString());
        //        this.CheckLogin();
        //    }
        //} 
        #endregion

        public void Step06()
        {
            if (ConfigurationSettings.AppSettings["Settings_site"].ToString() == "http://www.expedia.com/")
            {
                PaymentPage paymentPage = new PaymentPage_COM();
                selenium.Select(paymentPage.GetPageElementByLabel(PaymentPageElements.CardType), pcd.UserSelectedCardType);
                selenium.Type(paymentPage.GetPageElementByLabel(PaymentPageElements.CardNumber), pcd.UserCardNumber);
                selenium.Select(paymentPage.GetPageElementByLabel(PaymentPageElements.CardExpMonth), pcd.ExpirationDateMonth);
                selenium.Select(paymentPage.GetPageElementByLabel(PaymentPageElements.CardExpYear), pcd.ExpirationDateYear);
                selenium.Type(paymentPage.GetPageElementByLabel(PaymentPageElements.UserName), pcd.UserName);
                selenium.Type(paymentPage.GetPageElementByLabel(PaymentPageElements.UserSecondName), pcd.UserSecondName);
                selenium.Type(paymentPage.GetPageElementByLabel(PaymentPageElements.UserStreet), uai.Street);
                selenium.Type(paymentPage.GetPageElementByLabel(PaymentPageElements.UserCity), uai.City);
                selenium.Select(paymentPage.GetPageElementByLabel(PaymentPageElements.UserState), uai.State);
                selenium.Type(paymentPage.GetPageElementByLabel(PaymentPageElements.UserZipcode), uai.ZipCode);
                selenium.Type(paymentPage.GetPageElementByLabel(PaymentPageElements.UserArea), uai.Area);
                selenium.Type(paymentPage.GetPageElementByLabel(PaymentPageElements.UserPhone), uai.Phone);
                selenium.Type(paymentPage.GetPageElementByLabel(PaymentPageElements.UserExt), uai.Ext);
                selenium.Click(paymentPage.GetPageElementByLabel(PaymentPageElements.Confirm));
                selenium.WaitForPageToLoad(timeToWaitForPageLoad.ToString());
                this.CheckLogin();
            }
            else if (ConfigurationSettings.AppSettings["Settings_site"].ToString() == "http://www.expedia.be/")
            {
                UserDetailsPage detailsPage = new SecondDetailsPage_BE();
                selenium.Select(detailsPage.GetPageElementByLabel(UserDetailsPageElements.UserNameAndSecondName), pcd.UserName + " " + pcd.UserSecondName);
                selenium.Type(detailsPage.GetPageElementByLabel(UserDetailsPageElements.UserName), pcd.UserName);
                selenium.Type(detailsPage.GetPageElementByLabel(UserDetailsPageElements.UserSecondName), pcd.UserSecondName);
                selenium.Type(detailsPage.GetPageElementByLabel(UserDetailsPageElements.UserCountry), uai.Country);
                selenium.Click(detailsPage.GetPageElementByLabel(UserDetailsPageElements.Confirm));
                selenium.WaitForPageToLoad(timeToWaitForPageLoad.ToString());
                this.CheckLogin();
            }
        }

        public void Step07_BE()
        {
            if (ConfigurationSettings.AppSettings["Settings_site"].ToString() == "http://www.expedia.be/")
            {
                PaymentPage paymentPage = new PaymentPage_BE();
                selenium.Select(paymentPage.GetPageElementByLabel(PaymentPageElements.CardType), pcd.UserSelectedCardType);
                selenium.Type(paymentPage.GetPageElementByLabel(PaymentPageElements.CardNumber), pcd.UserCardNumber);
                selenium.Type(paymentPage.GetPageElementByLabel(PaymentPageElements.CardExpMonth), pcd.ExpirationDateMonth);
                selenium.Type(paymentPage.GetPageElementByLabel(PaymentPageElements.CardExpYear), pcd.ExpirationDateYear);
                selenium.Type(paymentPage.GetPageElementByLabel(PaymentPageElements.CVV), pcd.CVV);
                selenium.Type(paymentPage.GetPageElementByLabel(PaymentPageElements.UserName), pcd.UserName);
                selenium.Type(paymentPage.GetPageElementByLabel(PaymentPageElements.UserSecondName), pcd.UserSecondName);
                selenium.Type(paymentPage.GetPageElementByLabel(PaymentPageElements.UserStreet), uai.Street);
                selenium.Type(paymentPage.GetPageElementByLabel(PaymentPageElements.UserCity), uai.City);
                selenium.Type(paymentPage.GetPageElementByLabel(PaymentPageElements.UserState), uai.State);
                selenium.Type(paymentPage.GetPageElementByLabel(PaymentPageElements.UserZipcode), uai.ZipCode);
                selenium.Select(paymentPage.GetPageElementByLabel(PaymentPageElements.UserCountry), "value=USA");
                selenium.Type(paymentPage.GetPageElementByLabel(PaymentPageElements.UserArea), uai.Area);
                selenium.Type(paymentPage.GetPageElementByLabel(PaymentPageElements.UserPhone), uai.Phone);
                selenium.Type(paymentPage.GetPageElementByLabel(PaymentPageElements.UserExt), uai.Ext);
                selenium.Type(paymentPage.GetPageElementByLabel(PaymentPageElements.Email), lp.LoginName);
                selenium.Type(paymentPage.GetPageElementByLabel(PaymentPageElements.EmailConfirmation), lp.LoginName);
                selenium.Click(paymentPage.GetPageElementByLabel(PaymentPageElements.Confirm));
                selenium.WaitForPageToLoad(timeToWaitForPageLoad.ToString());
                this.CheckLogin();
            }
        }

        public void CloseSeleniumObject()
        {
            selenium.Stop();      
        }

        private void WaitForElement(double WaitTime, string ElementName)
        {
            DateTime start = DateTime.Now;
            TimeSpan ts = DateTime.Now.Subtract(start);
            bool bFound = false;
            do
            {
                try
                {
                    if (selenium.IsElementPresent(ElementName))
                    {
                        bFound = true;
                        break;
                    }
                }
                catch (Exception)
                { }
                System.Threading.Thread.Sleep(1000);
                ts = DateTime.Now.Subtract(start);
            }
            while (ts.TotalMilliseconds < WaitTime);

            if (!bFound) Assert.Fail("timeout");
        }

        private void WaitForPageHide(string PageTitle, double WaitTime)
        {
            DateTime start = DateTime.Now;
            TimeSpan ts = DateTime.Now.Subtract(start);
            do
            {
                try
                {
                    if (selenium.GetTitle() != PageTitle)
                    {
                        break;
                    }
                }
                catch (Exception)
                { }
                System.Threading.Thread.Sleep(1000);
                ts = DateTime.Now.Subtract(start);
            }
            while (ts.TotalMilliseconds < WaitTime);
        }

    }
}
